package com.smartmedical.common.core.domain;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.smartmedical.common.core.constant.Constants;
import com.smartmedical.common.core.constant.HealthConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author ruoyi
 */
@JsonPropertyOrder({  "result", "message","status","totalRecords" })
public class HealthR<T> implements Serializable
{
    private static final long serialVersionUID = 1L;

    /** 成功 */
    public static final String SUCCESS = HealthConstants.SUCCESS;

    /** 失败 */
    public static final String FAIL = HealthConstants.FAIL;

    private String status;

    private String message="成功";

    private Integer totalRecords=0;

    private T result;


    public static <T> HealthR<T> ok()
    {
        return restResult(null, SUCCESS, null);
    }

    public static <T> HealthR<T> ok(T result)
    {
        return restResult(result, SUCCESS, null);
    }



    public static <T> HealthR<T> ok(T result, String message)
    {
        return restResult(result, SUCCESS, message);
    }

    public static <T> HealthR<T> fail()
    {
        return restResult(null, FAIL, null);
    }

    public static <T> HealthR<T> fail(String message)
    {
        return restResult(null, FAIL, message);
    }

    public static <T> HealthR<T> fail(T result)
    {
        return restResult(result, FAIL, null);
    }

    public static <T> HealthR<T> fail(T result, String message)
    {
        return restResult(result, FAIL, message);
    }

    public static <T> HealthR<T> fail(String status, String message)
    {
        return restResult(null, status, message);
    }

    private static <T> HealthR<T> restResult(T result, String status, String message)
    {
        HealthR<T> apiResult = new HealthR<>();
        apiResult.setResult(result);
        apiResult.setStatus(status);
        if (StringUtils.isNotEmpty(message)){
            apiResult.setMessage(message);
        }
        return apiResult;
    }

    public String getStatus()
    {
        return status;
    }


    public Integer getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(Integer totalRecords) {
        this.totalRecords = totalRecords;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public T getResult()
    {
        return result;
    }

    public void setResult(T result)
    {
        this.result = result;
    }

    public static <T> Boolean isError(HealthR<T> ret)
    {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess(HealthR<T> ret)
    {
        return HealthR.SUCCESS .equals(ret.getStatus());
    }


}
